package com.isias.system.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isias.model.El.ElPaper;
import com.isias.model.El.ElQu;
import com.isias.model.El.ElRepo;
import com.isias.model.vo.ElPaperQueryVo;
import com.isias.model.vo.ElQuQueryVo;
import com.isias.model.vo.ElRepoQueryVo;
import com.isias.system.mapper.ElPaperMapper;
import com.isias.system.mapper.ElRepoMapper;
import com.isias.system.service.ElPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ElPaperServiceImpl extends ServiceImpl<ElPaperMapper, ElPaper> implements ElPaperService {

    @Autowired
    private ElPaperMapper elPaperMapper;


    @Override
    public IPage<ElPaper> SelectPage(Page<ElPaper> pageparam, ElPaperQueryVo elPaperQueryVo) {
        return elPaperMapper.SelectPage(pageparam,elPaperQueryVo);
    }

    @Override
    public List<ElQu> SelectQuByPaperId(String id, ElQuQueryVo elQuQueryVo) {
        return elPaperMapper.SelectQuByPaperId(id,elQuQueryVo);
    }

    @Override
    public void reFresh(String id) {
        //废弃方案、原因：如果出现空值，则顺序会乱，或者报错；加入单选题为0个，则查出来的只有三条数据，会出问题，这里改为一个一个查询（没有办法了，牺牲一些性能吧）
//        //获取各种题目对应的数量,从第一个到最后一个分别为：单选、多选、判断和论述
//        List<String> num = elPaperMapper.SelectQuNumber(id);

        //通过id和四种type获取到题目的类型，缺点是查询四次，还是两次连接查询，性能肯定不好呜呜呜呜呜呜呜呜呜呜
        int radio = elPaperMapper.SelectQuNumberByType(id, "1");    //单选
        int multi = elPaperMapper.SelectQuNumberByType(id, "2");       //多选
        int judge = elPaperMapper.SelectQuNumberByType(id, "3");    //判断
        int saq = elPaperMapper.SelectQuNumberByType(id, "4");      //论述
        //在数据库中对试卷表进行更新操作
        elPaperMapper.UpdatePaperQuNumber(radio,multi,judge,saq,id);

    }

    @Override
    public boolean AddQuToRepo(String paperId, String quId) {
        if (elPaperMapper.findIdByRepoIdAndQuId(paperId,quId) > 0){
            return false;
        }
        return elPaperMapper.AddQuToRepo(paperId,quId)>0;
    }

    @Override
    public boolean removeQuToPaper(String paperId, String quId) {
        return elPaperMapper.removeQuToPaper(paperId,quId) > 0 ;
    }

}
